package com.IUT.biblio;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.IUT.biblio.model.*;

public class ctrlLecteur extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	public void doGet(	HttpServletRequest request, 
			HttpServletResponse response)
			throws IOException, ServletException{
		
		if(getServletConfig().getInitParameter("action").toString().equals("add")){
			request.getRequestDispatcher("../ajoutLect.jsp").forward(request, response);
		}
		else if(getServletConfig().getInitParameter("action").toString().equals("consult")){
			request.getRequestDispatcher("../consultLect.jsp").forward(request, response);
		}
		else{
			request.setAttribute("error", "Action interdite : " + getServletConfig().getInitParameter("action").toString());
			request.getRequestDispatcher("../error.jsp").forward(request, response);
		}
		
	}
	
	public void doPost(     HttpServletRequest request, 
            HttpServletResponse response)
            throws IOException, ServletException{

		ServletContext context = getServletContext();
		
		if(request.getParameter("actForm")==null){
			request.setAttribute("error", "Erreur inconnue");
			request.getRequestDispatcher("../error.jsp").forward(request, response);
		}
		else if(request.getParameter("actForm").toString().equals("ajoutLect")){
			
			if(request.getParameter("nom").isEmpty() 
			|| request.getParameter("prenom").isEmpty() 
			|| request.getParameter("age").isEmpty() 
			|| request.getParameter("adresse").isEmpty() 
			|| request.getParameter("numTel").isEmpty()){
				
				request.setAttribute("error", "Tout les champs sont obligatoires !!!");
				request.getRequestDispatcher("../error.jsp").forward(request, response);
			}
			else{
				String nom = request.getParameter("nom").toString();
				String prenom = request.getParameter("prenom").toString();
				String adresse = request.getParameter("adresse").toString();
				int age = Integer.parseInt(request.getParameter("age").toString());
				String numTel = request.getParameter("numTel").toString();
							
				Bibliotheque biblio =(Bibliotheque)context.getAttribute("biblio");
				int num = biblio.numLecSuivant();
				Lecteur l = new Lecteur(num, nom, prenom, age, adresse, numTel);
				biblio.lierLecteur(num, l);
				request.setAttribute("texte", "Lecteur ajouté, id : " + num);
				request.setAttribute("save", "yes");
				request.getRequestDispatcher("/Biblio").forward(request, response);
			}
		}
		else if(request.getParameter("actForm").toString().equals("consultLect")){
			request.setAttribute("id", Integer.parseInt(request.getParameter("id").toString()));
			request.getRequestDispatcher("../consultLect.jsp").forward(request, response);
		}
		else{
			request.setAttribute("error", "Action interdite : " + getServletConfig().getInitParameter("action").toString());
			request.getRequestDispatcher("../error.jsp").forward(request, response);
		}
}

}
